package cn.stylefeng.guns.modular.eruser.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.eruser.entity.ErUser;
import cn.stylefeng.guns.modular.eruser.mapper.ErUserMapper;
import cn.stylefeng.guns.modular.eruser.model.param.ErUserParam;
import cn.stylefeng.guns.modular.eruser.service.ErUserService;
import cn.stylefeng.guns.modular.eruser.enums.ErUserExceptionEnum;
import cn.stylefeng.guns.sys.modular.file.service.SysFileInfoService;
import cn.stylefeng.guns.sys.modular.user.entity.SysUser;
import cn.stylefeng.guns.sys.modular.user.param.SysUserParam;
import cn.stylefeng.guns.sys.modular.user.result.SysUserResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户表 服务实现类
 *
 * @author w
 * @date 2021/06/16 16:27
 */
@Service
public class ErUserServiceImpl extends ServiceImpl<ErUserMapper, ErUser> implements ErUserService {

    SysFileInfoService sysFileInfoService;

    @Override
    public ErUser getUserByCount(String account) {
        LambdaQueryWrapper<ErUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ErUser::getAccount, account);
        return this.getOne(queryWrapper);
    }

    @Override
    public PageResult<ErUser> page(ErUserParam erUserParam) {

        // 构造条件
        LambdaQueryWrapper<ErUser> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(erUserParam)) {
            //根据关键字模糊查询（姓名，账号）
            if (ObjectUtil.isNotEmpty(erUserParam.getSearchValue())) {
                queryWrapper.and(q -> q.like(ErUser::getAccount, erUserParam.getSearchValue())
                        .or().like(ErUser::getName, erUserParam.getSearchValue()));
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<ErUser> list(ErUserParam erUserParam) {

        // 构造条件
        LambdaQueryWrapper<ErUser> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(ErUserParam erUserParam) {

        // 将dto转为实体
        ErUser erUser = new ErUser();
        BeanUtil.copyProperties(erUserParam, erUser);

        this.save(erUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(ErUserParam erUserParam) {
        this.removeById(erUserParam.getId());
    }

    @Override
    public void edit(ErUserParam erUserParam) {

        // 根据id查询实体
        ErUser erUser = this.queryErUser(erUserParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(erUserParam, erUser);

        this.updateById(erUser);
    }

    @Override
    public ErUser detail(ErUserParam erUserParam) {
        return this.queryErUser(erUserParam);
    }

    //更换头像
    @Override
    public void updateAvatar(ErUserParam erUserParam) {
        ErUser erUser = this.queryErUser(erUserParam);
        Long avatar = erUserParam.getAvatar();
        sysFileInfoService.assertFile(avatar);
        erUser.setAvatar(avatar);
        this.updateById(erUser);
    }


    /**
     * 获取用户表
     *
     * @author w
     * @since 2021/06/16 16:27
     */
    private ErUser queryErUser(ErUserParam erUserParam) {
        ErUser erUser = this.getById(erUserParam.getId());
        if (ObjectUtil.isEmpty(erUser)) {
            throw new ServiceException(ErUserExceptionEnum.NOT_EXISTED);
        }
        return erUser;
    }
}